<template>
  <div class="fillcontain">
    <bt-col-header title="新增规则动作"></bt-col-header>
    <div class="page-content">
      <!-- <div class="head-title">
        <i class="el-icon-vwedit">新增规则动作</i>
      </div> -->
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
        <div class="form-container">
          <div class="row">
            <el-form-item class="col-md-6" label="规则动作编码" label-width="120px" prop="divisorIdentify">
              <el-input v-model="dataForm.divisorIdentify" disabled placeholder="规则动作编码"></el-input>
            </el-form-item>
            <el-form-item class="col-md-6" label="规则动作名称" label-width="120px" prop="divisorName">
              <el-input v-model="dataForm.divisorName" placeholder="规则动作名称"></el-input>
            </el-form-item>
          </div>
          <div class="row">
            <el-form-item class="col-md-6" label="关联模型" label-width="120px" prop="entityId">
              <el-select v-model="dataForm.entityId" filterable placeholder="请选择">
                <el-option v-for="item in entitys" :key="item.entityId" :label="item.label" :value="item.entityId" @click.native="changeEntity(item)">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item class="col-md-6" label="因子类型" label-width="120px" prop="divisorType">
              <el-select v-model="dataForm.divisorType" filterable placeholder="请选择">
                <el-option v-for="item in divisorTypes" :key="item.value" :label="item.label" :value="item.value" @click.native="changeDivisorType(item)">
                </el-option>
              </el-select>
            </el-form-item>
          </div>
          <div class="row" v-if="dataForm.divisorType === '0' || dataForm.divisorType === '1'">
            <el-form-item v-if="dataForm.divisorType === '0'" class="col-md-6" label="存值方式" label-width="120px" prop="valueType">
              <el-select v-model="dataForm.valueType" filterable placeholder="请选择">
                <el-option v-for="item in valueTypes" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item v-if="dataForm.divisorType === '1'" class="col-md-6" label="方法选择" label-width="120px" prop="divisorMethod">
              <el-select v-model="dataForm.divisorMethod" filterable placeholder="请选择">
                <el-option v-for="item in methodList" :key="item.name" :label="item.className" :value="item.name" @click.native="changeMethod(item)">
                </el-option>
              </el-select>
            </el-form-item>
          </div>
          <div class="row" v-if="dataForm.divisorType === '1'">
            <el-form-item label="方法表达式" label-width="120px" prop="remark">
              <el-input type="textarea" disabled :rows="2" v-model="dataForm.methodExpression" placeholder="方法表达式"></el-input>
            </el-form-item>
            <el-form-item label="方法描述" label-width="120px" prop="remark">
              <el-input type="textarea" disabled :rows="2" v-model="dataForm.methodDesc" placeholder="方法描述"></el-input>
            </el-form-item>
          </div>
          <div class="row">
            <el-form-item class="col-md-6" label="是否配置" label-width="120px" prop="isConfig">
              <el-switch v-model="dataForm.isConfig" active-color="#13ce66" inactive-color="#ff4949" on-value="1" on-text="是" off-text="否" off-value="0">
              </el-switch>
            </el-form-item>
            <el-form-item class="col-md-6" label="是否有效" label-width="120px" prop="isEffect">
              <el-switch v-model="dataForm.isEffect" active-color="#13ce66" inactive-color="#ff4949" on-value="1" on-text="是" off-text="否" off-value="0">
              </el-switch>
            </el-form-item>
          </div>
          <div class="row">
            <el-form-item v-if="dataForm.divisorType === '3'" class="col-md-6" label="存值方式" label-width="120px" prop="valueType">
              <el-select v-model="dataForm.valueType" filterable placeholder="请选择">
                <el-option v-for="item in valueTypes" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </div>
          <div class="row" v-if="dataForm.divisorType === '3'">
            <el-form-item label="表达式" label-width="120px" prop="remark">
              <el-input type="textarea" :rows="2" v-model="dataForm.methodExpression" placeholder="表达式"></el-input>
            </el-form-item>
            <el-form-item label="描述" label-width="120px" prop="remark">
              <el-input type="textarea" :rows="2" v-model="dataForm.methodDesc" placeholder="描述"></el-input>
            </el-form-item>
          </div>
        </div>

        <div class="mb10 pl20" v-if="
            dataForm.divisorType === '1' &&
            dataForm.methodParams &&
            dataForm.methodParams.length
          ">
          <!-- <div class="content-title"><span>参数</span></div> -->
          <div class="padding_10_0">
            <bt-col-header title="参数"></bt-col-header>
          </div>
          <div class="page-content" v-for="(itemparam, itIndex) in dataForm.methodParams" :key="itIndex">
            <div class="form-container">
              <div class="row">
                <el-form-item class="col-md-6" label="" label-width="120px">
                  <el-select v-model="dataForm.methodParams[itIndex].valueType" filterable placeholder="值类型">
                    <el-option v-for="item in methodValueTypes" :key="item.value" :label="item.label" :value="item.value" @click.native="changeValueType(item, itIndex)">
                    </el-option>
                  </el-select>
                </el-form-item>
              </div>
            </div>
            <div class="mb10" v-if="dataForm.methodParams[itIndex].valueType === '2'">
              <!-- <div class="content-title"><span>新增值枚举</span></div> -->
              <div class="padding_10_0">
                <bt-col-header title="新增值枚举"></bt-col-header>
              </div>
              <div class="page-content">
                <div class="row mb10">
                  <el-button type="info" plain icon="circle-plus" @click="methodaddEnum(itemparam, itIndex)">新增</el-button>
                </div>
                <div class="">
                  <el-form ref="methodEnumFrom" :model="methodEnumFrom[itIndex]" label-width="80px">
                    <el-table :data="methodEnumFrom[itIndex].methodEnum" border style="width: 100%">
                      <el-table-column prop="enumCode" header-align="center" align="center" label="枚举代码">
                        <template slot-scope="scope">
                          <el-form-item :rules="itemRule.enumCode" :prop="'methodEnum.' + scope.$index + '.enumCode'">
                            <el-input v-model="scope.row.enumCode" placeholder="枚举代码"></el-input>
                          </el-form-item>
                        </template>
                      </el-table-column>
                      <el-table-column prop="enumValue" header-align="center" align="center" label="枚举描述">
                        <template slot-scope="scope">
                          <el-form-item :rules="itemRule.enumValue" :prop="'methodEnum.' + scope.$index + '.enumValue'">
                            <el-input v-model="scope.row.enumValue" placeholder="枚举描述"></el-input>
                          </el-form-item>
                        </template>
                      </el-table-column>
                      <el-table-column label="操作">
                        <template slot-scope="scope">
                          <el-button type="text" size="small" @click.native.prevent="
                              deleteItem(
                                scope.$index,
                                methodEnumFrom[itIndex].methodEnum
                              )
                            ">删除
                          </el-button>
                        </template>
                      </el-table-column>
                    </el-table>
                  </el-form>
                </div>
              </div>

            </div>
          </div>
        </div>

        <!--条件判断-->
        <div class="mb10 pl20" v-if="dataForm.divisorType === '0'">
          <!-- <div class="content-title"><span>条件判断</span></div> -->
          <div class="padding_10_0">
            <bt-col-header title="条件判断"></bt-col-header>
          </div>
          <div class="page-content">
            <div class="row">
              <el-form-item label="" prop="itemName" class="col-md-3">
                <el-input v-model="dataForm.itemName" readonly placeholder="关联属性"></el-input>
              </el-form-item>
              <el-form-item class="col-md-1" label-width="0px">
                <el-button :plain="true" type="info" @click="changeProperties">选择</el-button>
              </el-form-item>
              <el-form-item label="" prop="sysbol" class="col-md-4">
                <el-select v-model="dataForm.sysbol" filterable placeholder="逻辑符号">
                  <el-option v-for="item in sysbols" :key="item.value" :label="item.zh" :value="item.value" @click.native="changeSysbols(item)">
                  </el-option>
                </el-select>
              </el-form-item>
              <!-- 输入值-->
              <el-form-item v-if="dataForm.valueType === '2'" class="col-md-4" label="" prop="itemValueType">
                <el-select v-model="dataForm.itemValueType" filterable placeholder="值类型">
                  <el-option v-for="item in itemValueTypes" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item v-if="dataForm.valueType === '1'" class="col-md-4" label="" prop="itemValueType" :rules="{
                    required: true,
                    message: '请选择参数类型！',
                    trigger: 'change',
                  }">
                <el-select v-model="dataForm.itemValueType" filterable placeholder="值类型">
                  <el-option v-for="item in itemValueTypes1" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </div>

          <!--输入值 且 文本输入-->
          <div class="mb10" v-if="dataForm.valueType === '2' && dataForm.itemValueType === '1'">
            <!-- <div class="content-title"><span>文本输入</span></div> -->
            <div class="padding_10_0">
              <bt-col-header title="文本输入"></bt-col-header>
            </div>
            <div class="page-content">
              <div class="row mb10">
                <el-form-item label="文本类型" class="col-md-4" prop="itemTxtType">
                  <el-select v-model="dataForm.itemTxtType" filterable placeholder="文本类型">
                    <el-option v-for="item in itemTxtTypes" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item v-if="dataForm.itemTxtType != '2'" class="col-md-4" label="字段长度" prop="itemTxtLength">
                  <el-input v-model="dataForm.itemTxtLength" type="number" placeholder="字段长度"></el-input>
                </el-form-item>
              </div>
            </div>

          </div>

          <!--固定值 且 文本输入-->
          <div class="mb10" v-if="dataForm.valueType === '1' && dataForm.itemValueType === '1'">
            <!-- <div class="content-title"><span>文本输入</span></div> -->
            <div class="padding_10_0">
              <bt-col-header title="文本输入"></bt-col-header>
            </div>
            <div class="page-content">
              <div class="row">
                <el-form-item class="col-md-4" label="文本值" prop="rightValue">
                  <el-input v-model="dataForm.rightValue" placeholder="文本值"></el-input>
                </el-form-item>
              </div>
            </div>

          </div>

          <!--固定值 且 模型属性-->
          <div class="mb10" v-if="dataForm.valueType === '1' && dataForm.itemValueType === '3'">
            <!-- <div class="content-title"><span>右模型</span></div> -->
            <div class="padding_10_0">
              <bt-col-header title="右模型"></bt-col-header>
            </div>
            <div class="page-content">
              <div class="row">
                <el-form-item class="col-md-4" label="右模型" prop="rightEntityId">
                  <el-select v-model="dataForm.rightEntityId" filterable placeholder="右模型">
                    <el-option v-for="item in entitys" :key="item.entityId" :label="item.label" :value="item.entityId" @click.native="changeRightEntity(item)">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item class="col-md-4" label="右属性" prop="rightProperty">
                  <el-select v-model="dataForm.rightProperty" filterable placeholder="右属性">
                    <el-option v-for="item in entityItemInfos" :key="item.itemId" :label="item.label" :value="item.itemId" @click.native="checkRightItem(item)">
                    </el-option>
                  </el-select>
                </el-form-item>
              </div>
            </div>
          </div>

          <!--枚举-->
          <div class="mb10" v-if="dataForm.valueType === '2' && dataForm.itemValueType === '2'">
            <!-- <div class="content-title"><span>新增值枚举</span></div> -->
            <div class="padding_10_0">
              <bt-col-header title="新增值枚举"></bt-col-header>
            </div>
            <div class="page-content">
              <div class="row mb10">
                <el-button type="info" icon="circle-plus" plain @click="addEnum">新增</el-button>
              </div>
              <div class="">
                <el-form ref="dataItemForm" :model="itemDataForm" label-width="80px">
                  <el-table :data="itemDataForm.enumList" border style="width: 100%">
                    <el-table-column prop="enumCode" header-align="center" align="center" label="枚举代码">
                      <template slot-scope="scope">
                        <el-form-item :rules="itemRule.enumCode" :prop="'enumList.' + scope.$index + '.enumCode'">
                          <el-input v-model="scope.row.enumCode" placeholder="枚举代码"></el-input>
                        </el-form-item>
                      </template>
                    </el-table-column>
                    <el-table-column prop="enumValue" header-align="center" align="center" label="枚举描述">
                      <template slot-scope="scope">
                        <el-form-item :rules="itemRule.enumValue" :prop="'enumList.' + scope.$index + '.enumValue'">
                          <el-input v-model="scope.row.enumValue" placeholder="枚举描述"></el-input>
                        </el-form-item>
                      </template>
                    </el-table-column>
                    <el-table-column label="操作">
                      <template slot-scope="scope">
                        <el-button type="text" size="small" @click.native.prevent="
                            deleteItem(scope.$index, itemDataForm.enumList)
                          ">删除
                        </el-button>
                      </template>
                    </el-table-column>
                  </el-table>
                </el-form>
              </div>
            </div>
          </div>
        </div>

        <!--自定义方法-->
        <div class="mb10 pl20" v-if="dataForm.divisorType === '3'">
          <bt-col-header title="参数"></bt-col-header>
          <div class="page-content">
            <div class="row mb10">
              <el-button type="info" icon="circle-plus" plain @click="addParams()">新增</el-button>
            </div>
            <!-- <div class="content-title">
            <span>参数</span
            ><i class="el-icon-circle-plus-outline ml10" @click="addParams"></i>
          </div> -->
            <div v-for="(itemparam, itIndex) in dataForm.methodParams" :key="itIndex">
              <div class="form-container">
                <div class="row">
                  <el-form-item class="col-md-3">
                    <el-input v-model="dataForm.methodParams[itIndex].name" placeholder="参数"></el-input>
                  </el-form-item>
                  <el-form-item class="col-md-3">
                    <el-input v-model="dataForm.methodParams[itIndex].desc" placeholder="参数描述"></el-input>
                  </el-form-item>
                  <el-form-item class="col-md-3">
                    <el-select v-model="dataForm.methodParams[itIndex].valueType" filterable placeholder="值类型">
                      <el-option v-for="item in methodValueTypes" :key="item.value" :label="item.label" :value="item.value" @click.native="changeValueType(item, itIndex)">
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label-width="0px" class="col-md-2">
                    <el-button icon="delete" circle @click="deleteParams(itIndex)"></el-button>
                  </el-form-item>
                </div>
              </div>
              <div class="mb10" v-if="dataForm.methodParams[itIndex].valueType === '2'">
                <!-- <div class="content-title"><span>新增值枚举</span></div> -->
                <div class="padding_10_0">
                  <bt-col-header title="新增值枚举"></bt-col-header>
                </div>
                <div class="page-content">
                  <div class="row mb10">
                    <el-button type="info" plain icon="circle-plus" @click="methodaddEnum(itemparam, itIndex)">新增</el-button>
                  </div>
                  <div class="">
                    <el-form ref="methodEnumFrom" :model="methodEnumFrom[itIndex]" label-width="80px">
                      <el-table :data="methodEnumFrom[itIndex].methodEnum" border style="width: 100%">
                        <el-table-column prop="enumCode" header-align="center" align="center" label="枚举代码">
                          <template slot-scope="scope">
                            <el-form-item :rules="itemRule.enumCode" :prop="'methodEnum.' + scope.$index + '.enumCode'">
                              <el-input v-model="scope.row.enumCode" placeholder="枚举代码"></el-input>
                            </el-form-item>
                          </template>
                        </el-table-column>
                        <el-table-column prop="enumValue" header-align="center" align="center" label="枚举描述">
                          <template slot-scope="scope">
                            <el-form-item :rules="itemRule.enumValue" :prop="'methodEnum.' + scope.$index + '.enumValue'">
                              <el-input v-model="scope.row.enumValue" placeholder="枚举描述"></el-input>
                            </el-form-item>
                          </template>
                        </el-table-column>
                        <el-table-column label="操作">
                          <template slot-scope="scope">
                            <el-button type="text" size="small" @click.native.prevent="
                              deleteItem(
                                scope.$index,
                                methodEnumFrom[itIndex].methodEnum
                              )
                            ">删除</el-button>
                          </template>
                        </el-table-column>
                      </el-table>
                    </el-form>
                  </div>
                </div>

              </div>
            </div>
          </div>

        </div>
      </el-form>

      <!--属性选择-->
      <el-dialog title="提示" :visible.sync="dialogVisible" width="70%" append-to-body>
        <el-form :inline="true" :model="itemDataForm" ref="itemDataForm">
          <el-form-item label="属性代码">
            <el-input v-model="itemDataForm.itemIdentify" placeholder="属性代码" clearable></el-input>
          </el-form-item>
          <el-form-item label="字段类型">
            <el-select v-model="itemDataForm.itemType" filterable clearable placeholder="字段类型">
              <el-option v-for="item in entityItemType" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <!-- <el-button @click="getEntityProperties()">查询</el-button>
            <el-button type="primary" size="small" @click="resetData"
              >重置</el-button
            > -->
            <rich-button :plain="true" type="info" shape="capsule" icon="search" @click="getEntityProperties()">查询</rich-button>
            <rich-button shape="capsule" icon="quit" @click="resetData">重置</rich-button>
          </el-form-item>
        </el-form>
        <!-- 列表 -->
        <el-table :data="itemDataList" ref="multipleTable" border style="width: 100%" @select="handleSelectionChange">
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column prop="entityIdentify" header-align="center" align="center" width="120" label="模版名称">
          </el-table-column>
          <el-table-column prop="itemIdentify" header-align="center" align="center" label="属性代码">
          </el-table-column>
          <el-table-column prop="itemName" header-align="center" align="center" label="中文名称">
          </el-table-column>
          <el-table-column prop="itemType" header-align="center" align="center" label="字段类型">
            <template slot-scope="scope">
              {{ scope.row.itemType }}
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination class="float-right mt-2" @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="page.pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="page.pageSize" :total="page.totalPage" layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>

        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">返回</el-button>
          <el-button :plain="true" type="info" @click="checkItem()">确定</el-button>
        </span>
      </el-dialog>

      <div class="content-footer">
        <el-button :plain="true" type="info" @click="dataFormSubmit()">确定</el-button>
        <el-button @click="toList">取消</el-button>
      </div>
    </div>
  </div>

</template>

<script>
import {
  entityItemList,
  entityList,
  pageItems,
  getActionMethodName,
} from "@/api/baseConfig/entity";
import { saveData, getActionData } from "@/api/baseConfig/actionDivisor";
import constant from "../../../../../api/constant";

export default {
  data() {
    var validateitemName = (rule, value, callback) => {
      if (this.dataForm.divisorType === "0") {
        if (
          this.dataForm.itemIdentify === "" ||
          this.dataForm.itemIdentify == undefined
        ) {
          callback(new Error("关联属性不能为空"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    var validatedivisorMethod = (rule, value, callback) => {
      if (this.dataForm.divisorType === "1") {
        if (value === "" || value == undefined) {
          callback(new Error("方法不能为空"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    var validatesysbol = (rule, value, callback) => {
      if (this.dataForm.divisorType === "0") {
        if (value === "" || value == undefined) {
          callback(new Error("逻辑符号不能为空"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    var validateitemValueType = (rule, value, callback) => {
      if (this.dataForm.divisorType === "0") {
        if (value === "" || value == undefined) {
          callback(new Error("属性值不能为空"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    var validateitemTxtType = (rule, value, callback) => {
      if (
        this.dataForm.divisorType === "0" &&
        this.dataForm.itemValueType === "1"
      ) {
        if (value === "" || value == undefined) {
          callback(new Error("文本类型不能为空"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    var validateitemTxtLength = (rule, value, callback) => {
      if (
        this.dataForm.divisorType === "0" &&
        this.dataForm.itemValueType === "1" &&
        this.dataForm.itemTxtType &&
        this.dataForm.itemTxtType != "2"
      ) {
        if (value === "" || value == undefined) {
          callback(new Error("字段长度不能为空"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    var validateenumValue = (rule, value, callback) => {
      if (
        this.dataForm.divisorType === "0" &&
        this.dataForm.itemValueType === "2"
      ) {
        if (value === "" || value == undefined) {
          callback(new Error("枚举描述不能为空"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    var validateenumCode = (rule, value, callback) => {
      if (
        this.dataForm.divisorType === "0" &&
        this.dataForm.itemValueType === "2"
      ) {
        if (value === "" || value == undefined) {
          callback(new Error("枚举代码不能为空"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    var validatevalueType = (rule, value, callback) => {
      if (this.dataForm.divisorType === "0") {
        if (value === "" || value == undefined) {
          callback(new Error("存值方式不能为空"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    var validaterightValue = (rule, value, callback) => {
      if (
        this.dataForm.valueType === "1" &&
        this.dataForm.itemValueType === "1"
      ) {
        if (value === "" || value == undefined) {
          callback(new Error("值不能为空"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    var validaterightEntityId = (rule, value, callback) => {
      if (
        this.dataForm.valueType === "1" &&
        this.dataForm.itemValueType === "3"
      ) {
        if (value === "" || value == undefined) {
          callback(new Error("右模型不能为空"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    var validaterightProperty = (rule, value, callback) => {
      if (
        this.dataForm.valueType === "1" &&
        this.dataForm.itemValueType === "3"
      ) {
        if (value === "" || value == undefined) {
          callback(new Error("右属性不能为空"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    return {
      methodValueTypes: [
        { label: "文本输入", value: "1" },
        { label: "新增枚举", value: "2" },
      ],
      methodEnumFrom: [
        {
          methodEnum: [],
        },
      ],
      entityItemInfos: [],
      itemRule: {
        enumValue: [{ validator: validateenumValue, trigger: "blur" }],
        enumCode: [{ validator: validateenumCode, trigger: "blur" }],
      },
      descData: [],
      dialogVisible: false,
      itemData: {},
      itemDataForm: {
        itemIdentify: "",
        itemType: "",
      },
      itemDataList: [],
      methodList: [],
      itemValueTypes: constant.ruleConfig.itemValueTypes,
      itemValueTypes1: constant.ruleConfig.itemValueTypes1,
      itemTxtTypes: constant.ruleConfig.itemTxtTypes,
      divisorTypes: constant.ruleConfig.divisorTypes,
      valueTypes: constant.ruleConfig.valueTypes,
      entityItemType: constant.ruleConfig.entityItemType,
      sysbols: constant.ruleConfig.actionSysbols,
      itemDataForm: {
        enumList: [],
      },
      visible: false,
      dataForm: {
        id: null,
        valueType: "",
        rightEntityId: null,
        rightProperty: null,
        rightValue: null,
        divisorIdentify: "",
        divisorName: "",
        divisorType: "",
        entityId: null,
        itemId: null,
        itemIdentify: null,
        itemName: null,
        divisorMethod: "",
        methodExpression: "",
        methodDesc: "",
        methodParams: "",
        methodType: "",
        itemValueType: "",
        itemTxtType: "",
        itemTxtLength: null,
        sysbol: "",
        remark: "",
        isEffect: "1",
        isConfig: "0",
      },
      isEffect: "1",
      page: {
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
      },
      entitys: [],
      dataRule: {
        divisorName: [
          { required: true, message: "因子名称不能为空", trigger: "change" },
        ],
        entityId: [
          { required: true, message: "关联模型不能为空" },
        ],
        divisorType: [
          { required: true, message: "因子类型不能为空", trigger: "change" },
        ],
        itemName: [{ validator: validateitemName, trigger: "blur" }],
        sysbol: [{ validator: validatesysbol, trigger: "blur" }],
        itemValueType: [{ validator: validateitemValueType, trigger: "blur" }],
        itemTxtType: [{ validator: validateitemTxtType, trigger: "blur" }],
        itemTxtLength: [{ validator: validateitemTxtLength, trigger: "blur" }],
        divisorMethod: [
          { validator: validatedivisorMethod, trigger: "change" },
        ],
        valueType: [{ validator: validatevalueType, trigger: "change" }],
        rightValue: [{ validator: validaterightValue, trigger: "change" }],
        rightEntityId: [
          { validator: validaterightEntityId, trigger: "change" },
        ],
        rightProperty: [
          { validator: validaterightProperty, trigger: "change" },
        ],
      },
    };
  },
  created() {
    let id = this.$route.params.id;
    this.init(id);
    this.getEntityCls();
  },
  activated() {
    let id = this.$route.params.id;
    this.init(id);
    this.getEntityCls();
  },
  methods: {
    addParams() {
      if (this.dataForm.methodParams) {
        this.dataForm.methodParams.push({ name: "", desc: "", valueType: "" });
      } else {
        this.dataForm.methodParams = [];
        this.dataForm.methodParams.push({ name: "", desc: "", valueType: "" });
      }
    },
    deleteParams(index) {
      this.dataForm.methodParams.splice(index, 1);
    },
    changeValueType(item, index) {
      // console.log(item);
      this.methodEnumFrom[index] = {};
      this.$set(this.methodEnumFrom[index], "methodEnum", []);
    },
    changeRightEntity(item) {
      this.descData[3] = { k: item.entityIdentify, v: item.entityName };
      let params = {
        id: item.entityId + "",
      };
      // 获取实体类属性
      entityItemList(params).then((res) => {
        this.entityItemInfos = [];
        if (res.data.data) {
          res.data.data.forEach((item) => {
            item["label"] = item.itemName + "--" + item.itemIdentify;
            this.entityItemInfos.push(item);
          });
        }
      });
    },
    checkRightItem(item) {
      this.descData[4] = { k: item.itemIdentify, v: item.itemName };
    },
    toList() {
      constant.clearTag(this);
      this.$router.push({ name: constant.routerName.listActionDivisor });
    },
    changeSysbols(item) {
      this.descData[2] = { k: item.label, v: item.zh };
    },
    // 参数选择
    changParam(value, index) {
      // console.log(value, index, "222");
    },
    //方法选择
    changeMethod(item) {
      // console.log(item, "item");
      this.dataForm.methodExpression = item.name;
      this.dataForm.methodDesc = item.className;
      this.dataForm.methodParams = item.params;
      this.dataForm.methodType = item.type;
    },

    // 因子类型改变
    changeDivisorType(data) {
      if (!this.dataForm.entityId) {
        this.$message.error("请先绑定实体");
        this.dataForm.divisorType = "";
        return;
      }
      // this.dataForm.methodParams = [];
      // this.dataForm.itemValueType = "";
      // this.dataForm.divisorMethod = "";
      // this.dataForm.methodExpression = "";
      // this.dataForm.methodDesc = "";
      // 因子类型为方法获取实体类的方法
      if (this.dataForm.divisorType === "1") {
        this.dataForm.valueType = "";
        let params1 = {
          id: this.dataForm.entityId + "",
        };

        // 获取方法
        getActionMethodName(params1).then((res) => {
          this.methodList = res.data.data;
        });
      }
    },
    //重置属性查询
    resetData() {
      this.itemDataForm.itemIdentify = "";
      this.itemDataForm.itemType = "";
    },

    //确定选择属性
    checkItem() {
      this.dataForm.itemIdentify = this.itemData.itemIdentify;
      this.dataForm.itemId = this.itemData.itemId;
      this.dataForm.itemName = this.itemData.itemName;
      this.descData[1] = {
        k: this.itemData.itemIdentify,
        v: this.itemData.itemName,
      };
      this.dialogVisible = false;
    },
    // 属性选择
    handleSelectionChange(selection, row) {
      this.$refs.multipleTable.clearSelection();
      if (selection.length === 0) {
        // 判断selection是否有值存在
        this.itemData = {};
        return;
      }
      // console.log('点击', selection, row);
      this.itemData = row;
      this.$refs.multipleTable.toggleRowSelection(row, true);
    },
    //弹出属性选择框
    changeProperties() {
      if (!this.dataForm.entityId) {
        this.$message.error("请先绑定实体");
        return;
      }
      let params = {
        pageNumber: this.page.pageIndex,
        pageSize: this.page.pageSize,
        entityId: this.dataForm.entityId + "",
      };
      pageItems(params).then((res) => {
        if (res.data.data) {
          this.itemDataList = res.data.data.list;
          this.page.totalPage = res.data.data.totalCount;
        }
      });
      this.dialogVisible = true;
    },
    //方法新增枚举
    methodaddEnum(item, itIndex) {
      // console.log(item, "item");
      let params = {
        id: "",
        paramName: item.name, //参数名称
        divisorIdentify: "", //因子标识
        enumCode: "",
        enumValue: "",
      };
      this.methodEnumFrom[itIndex].methodEnum.push(params);
      // console.log(this.methodEnumFrom, "this.methodEnumFrom");
    },
    //新增枚举
    addEnum() {
      let params = {
        enumCode: "",
        enumValue: "",
      };
      this.itemDataForm.enumList.push(params);
    },
    //初始化
    init(id) {
      this.visible = true;
      this.itemDataForm.enumList = [];
      this.methodList = [];
      this.itemDataList = [];
      this.descData = new Array(5);
      this.dataForm = {
        id: null,
        divisorIdentify: "",
        divisorName: "",
        divisorType: "",
        entityId: null,
        itemId: null,
        itemIdentify: null,
        itemName: null,
        divisorMethod: "",
        methodExpression: "",
        methodDesc: "",
        methodParams: "",
        methodType: "",
        itemValueType: "",
        itemTxtType: "",
        itemTxtLength: null,
        sysbol: "",
        remark: "",
        isEffect: "1",
        isConfig: "0",
      };

      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (id && id != "undefined") {
          this.dataForm.id = id;
          let params = {
            id: this.dataForm.id + "",
          };
          this.$store.dispatch("loading/CHANGE_LOADING", true);
          getActionData(params)
            .then((res) => {
              this.$store.dispatch("loading/CHANGE_LOADING", false);
              if (res.data.data && res.data.resultCode === 0) {
                this.dataForm = res.data.data.dataForm;
                if (this.dataForm.methodParams) {
                  this.dataForm.methodParams = JSON.parse(
                    this.dataForm.methodParams
                  );
                  let methodEnum = res.data.data.methodEnum;
                  this.methodEnumFrom = [];
                  this.dataForm.methodParams.forEach((e, i) => {
                    this.methodEnumFrom[i] = {
                      methodEnum: [],
                    };
                    if (e.valueType == "2") {
                      let value = this.getmethodEnum(e.name, methodEnum);
                      this.methodEnumFrom[i].methodEnum = value;
                    }
                  });
                  // console.log(this.methodEnumFrom);
                }
                this.itemDataForm.enumList = res.data.data.enumList;
                if (this.dataForm.divisorType === "0") {
                  this.descData = JSON.parse(this.dataForm.remark);
                  this.dataForm.itemName = this.descData[1].v;
                  if (
                    this.dataForm.valueType === "1" &&
                    this.dataForm.itemValueType === "3"
                  ) {
                    let params = {
                      id: this.dataForm.rightEntityId + "",
                    };
                    // 获取实体类属性
                    entityItemList(params).then((res) => {
                      this.entityItemInfos = [];
                      if (res.data.data) {
                        res.data.data.forEach((item) => {
                          item["label"] =
                            item.itemName + "--" + item.itemIdentify;
                          this.entityItemInfos.push(item);
                        });
                      }
                    });
                  }
                }
                // 因子类型为方法获取实体类的方法
                if (this.dataForm.divisorType === "1") {
                  let params1 = {
                    id: this.dataForm.entityId + "",
                  };

                  // 获取方法
                  getActionMethodName(params1).then((res) => {
                    this.methodList = res.data.data;
                  });
                }
              }
            })
            .catch((err) => {
              this.$store.dispatch("loading/CHANGE_LOADING", false);
            });
        }
      });
    },
    getmethodEnum(name, data) {
      let param = [];
      data.forEach((n) => {
        if (n.paramName === name) {
          param.push(n);
        }
      });
      return param;
    },
    // 实体类改变
    changeEntity(item) {
      this.dataForm.entityIdentify = item.name;
      this.dataForm.pkgName = item.className;
      this.descData[0] = { k: item.entityIdentify, v: item.entityName };
      this.itemDataList = [];

      this.dataForm.methodExpression = "";
      this.dataForm.methodDesc = "";
      this.dataForm.itemName = "";
      this.dataForm.sysbol = "";
      this.dataForm.itemValueType = "";
      this.dataForm.itemTxtType = "";
      this.dataForm.itemTxtLength = "";

      this.changeDivisorType();
    },

    // 获取实体类列表
    getEntityCls() {
      entityList({}).then((res) => {
        this.entitys = [];
        if (res.data.data) {
          res.data.data.forEach((item) => {
            item["label"] = item.entityName + "-" + item.entityIdentify;
            this.entitys.push(item);
          });
        }
      });
    },

    // 每页数
    sizeChangeHandle(val) {
      this.page.pageSize = val;
      this.page.pageIndex = 1;
      this.getEntityProperties();
    },
    // 当前页
    currentChangeHandle(val) {
      this.page.pageIndex = val;
      this.getEntityProperties();
    },
    // 获取实体类的属性
    getEntityProperties() {
      let params = {
        pageNumber: this.page.pageIndex,
        pageSize: this.page.pageSize,
        entityId: this.dataForm.entityId + "",
        itemIdentify: this.itemDataForm.itemIdentify,
        itemType: this.itemDataForm.itemType,
      };
      pageItems(params).then((res) => {
        if (res.data.data) {
          this.itemDataList = res.data.data.list;
          this.page.totalPage = res.data.data.totalCount;
        }
      });
    },

    deleteItem(index, rows) {
      this.$confirm(`确定要删除吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          rows.splice(index, 1);
        })
        .catch(() => { });
    },

    // 表单提交
    dataFormSubmit() {
      // debugger
      if (this.dataForm.divisorType === "0") {
        this.dataForm.remark = JSON.stringify(this.descData);
      }
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          if (this.dataForm.divisorType === "0" && this.dataForm.itemValueType === "2") {
            this.$refs["dataItemForm"].validate((valid1) => {
              if (valid1) {
                this.saveInfo();
              }
            });
          } else {
            this.saveInfo();
          }
        }
      });
    },
    saveInfo() {
      let methodEnum = [];
      this.methodEnumFrom.forEach((e) => {
        methodEnum.push(...e.methodEnum);
      });
      if (this.dataForm.methodParams && this.dataForm.methodParams.length === 0) {
        this.dataForm.methodParams = null;
      }
      // console.log(methodEnum, "methodEnum");
      let params = {
        dataForm: JSON.stringify(this.dataForm),
        enumList: JSON.stringify(this.itemDataForm.enumList),
        methodEnum: JSON.stringify(methodEnum),
      };
      this.$store.dispatch("loading/CHANGE_LOADING", true);
      saveData(params)
        .then((res) => {
          this.$store.dispatch("loading/CHANGE_LOADING", false);
          if (res.data && res.data.resultCode === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.visible = false;
                this.dataForm = {
                  id: null,
                  divisorIdentify: "",
                  divisorName: "",
                  divisorType: "",
                  entityId: null,
                  itemId: null,
                  itemIdentify: null,
                  itemName: null,
                  divisorMethod: "",
                  methodExpression: "",
                  methodDesc: "",
                  methodParams: "",
                  methodType: "",
                  itemValueType: "",
                  itemTxtType: "",
                  itemTxtLength: null,
                  sysbol: "",
                  remark: "",
                  isEffect: "1",
                  isConfig: "0",
                };
                // this.$emit('refreshDataList')
                this.toList();
              },
            });
          } else {
            this.$message.error(res.data.resultMsg);
          }
        })
        .catch((err) => {
          this.$store.dispatch("loading/CHANGE_LOADING", false);
          // this.$message.error('请求失败！')
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.title {
  padding: 10px;
  background-color: rgba(248, 248, 248, 1);
  margin-bottom: 10px;
}

.el-table__row .el-form-item__content {
  margin-left: 0 !important;
}

.el-table__row .el-form-item {
  margin-bottom: 18px !important;
  margin-top: 18px !important;
}
</style>
